#pragma once

#include <QObject>
#include <QThread>
#include <QTimer>
#include <AlgManager/CHVisionAdvX.h>
#include <AlgManager/CPlateB.h>
#include <AlgManager/CPreProcess.h>
#include <AlgManager/DataX.h>
#include <Common/SysEnv.h>
#include <CameraManager/imagewindow.h>
#include <CameraManager/cameraservice.h>

class CameraService;
class AlgorithmService;
// 处理线程类 - 移到 AlgorithmService 内部
class ProcessingThread : public QThread
{
    Q_OBJECT
public:
    explicit ProcessingThread(AlgorithmService* algorithmService, QObject *parent = nullptr);
    ~ProcessingThread();
    void setProcessingData(const s_Image3dS &image);

protected:
    void run() override;

signals:
    void processingFinished(bool success, const QString &message);
    void progressUpdated(int step, const QString &status);
    void preProcessFinished(const s_PreProcess3DSResultPara &result);
    void defectDetectionFinished(const s_DefectPlateBRtsPara &result);
    void pathPlanningFinished(const s_LzppRtsPara &result);
private:
    AlgorithmService* m_algorithmService;
private:
    s_Image3dS m_image;
};

class AlgorithmService : public QObject
{
    Q_OBJECT

public:
        // 修改构造函数，添加相机服务参数
    explicit AlgorithmService(ImageWindow* imageWindow, CameraService* cameraService = nullptr, QObject* parent = nullptr);
    ~AlgorithmService();


    // 添加设置相机服务的方法
    Q_INVOKABLE void setCameraService(CameraService* cameraService);

    // 添加拍照方法
    bool captureImage();
    bool hasCamera() const;
private:
    // 添加相机服务指针
    CameraService* m_cameraService;





public:
    Q_INVOKABLE void stopPathAnimation();

    // 图像处理接口
    Q_INVOKABLE void load3DImage(const QString& filePath);
    Q_INVOKABLE void preProcess();
    Q_INVOKABLE void defectDetection();
    Q_INVOKABLE void pathPlanning();
    Q_INVOKABLE void runAllProcessing();
    Q_INVOKABLE void executePathAnimation();
    Q_INVOKABLE void saveParameters();

    // 状态查询
    Q_INVOKABLE bool hasImage() const { return m_currentImage.ID >= 0; }
    Q_INVOKABLE bool hasPreProcessResult() const { return m_preProcessResult.bTJG; }
    Q_INVOKABLE bool hasDefectResult() const { return m_defectResult.bTJG; }
    Q_INVOKABLE bool hasPathResult() const { return m_pathResult.bTJG; }
public:
    Q_INVOKABLE void savePathResultToJson(const s_LzppRtsPara& pathResult, s_DefectPlateBRtsPara &defectResult, const QString& filePath = "D:/SaveData/alg_result.json");


    // 数据获取
    s_Image3dS getCurrentImage() const { return m_currentImage; }
    s_PreProcess3DSResultPara getPreProcessResult() const { return m_preProcessResult; }
    s_DefectPlateBRtsPara getDefectResult() const { return m_defectResult; }
    s_LzppRtsPara getPathResult() const { return m_pathResult; }
    QVector<QString> getPathCommands() const { return m_commands; }


    void updateParameterDisplay();

    void parsePathPoint(s_LzppRtsPara pathResult);

signals:
    // 进度信号
    void progressUpdated(int step, const QString& status);

    // 结果显示信号
    void resultsUpdated();

    // 日志信号
    void logMessage(const QString& message);

private slots:
    void onProcessingFinished(bool success, const QString& message);
    void onProgressUpdated(int step, const QString& status);
    void onPreProcessFinished(const s_PreProcess3DSResultPara& result);
    void onDefectDetectionFinished(const s_DefectPlateBRtsPara& result);
    void onPathPlanningFinished(const s_LzppRtsPara& result);

public slots:
    void onFrameAcquired(const s_Image3dS& frame);

private:
    void setupConnections();
    void generatePathVisualization();
    void displayResults();
    void log(const QString& message);
    void clearResults();
    void onPathAnimationFinished(const HObject& GrayXYZ);
private:
    // 算法对象
    CHVisionAdvX m_visionAdv;
    CPreProcess m_preProcess;
    CPlateB m_plateB;

    // 处理数据
    s_Image3dS m_currentImage;
    s_PreProcess3DSResultPara m_preProcessResult;
    s_DefectPlateBRtsPara m_defectResult;
    s_LzppRtsPara m_pathResult;

    // 处理线程
    ProcessingThread* m_processingThread;

    // 图像显示
    ImageWindow* m_imageWindow;

    // 路径相关
    QVector<QString> m_commands;
    HObject m_pathCross;
    HObject m_pathPtsQgNotSafe;
    HObject m_pathArrowList;

private:
    // 路径动画相关
    QTimer* m_animationTimer;
    int m_currentAnimationIndex;
    int m_animationTotalSteps;
    HObject m_animationGrayXYZ;
private slots:
    void onAnimationTimeout();
public:
    // 分组相关方法
    Q_INVOKABLE int getCommandGroupCount() const { return m_commandGroups.size(); }
    Q_INVOKABLE QVector<QVector<QString>> getCommandGroups() const { return m_commandGroups; }
    Q_INVOKABLE bool hasCommandGroups() const { return !m_commandGroups.isEmpty(); }

    void groupCommandsByCylinder();

private:
    // 新增分组相关成员变量
    QVector<QVector<QString>> m_commandGroups;  // 分组后的指令
};
